﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using Microsoft.Win32;

namespace SkypeHistory
{
    // TODO: Scan all users directories if current user have privelegies

    /// <summary>
    /// Interaction logic for OpenDatabaseWindow.xaml
    /// </summary>
    public partial class OpenDatabaseWindow
    {
        public string FilePath { get; set; }
        public List<string> Profiles { get; set; } 
        private readonly Dictionary<string, string> _profilePaths; 

        public OpenDatabaseWindow()
        {
            Profiles = new List<string>();
            _profilePaths = new Dictionary<string, string>();
            InitializeComponent();
            LoadProfilePaths();
        }

        /// <summary>
        /// Scans AppData directory for existed skype profiles (by searching 'main.db' files)
        /// </summary>
        private void LoadProfilePaths()
        {
            try
            {
                var initialPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" + "Skype";
                var dirInfo = new DirectoryInfo(initialPath);
                var databases = dirInfo.GetFiles("main.db", SearchOption.AllDirectories);
                foreach (var database in databases)
                {
                    if (database.DirectoryName == null) continue;
                    var splited = database.DirectoryName.Split('\\');
                    var profile = splited[splited.Length - 1];
                    _profilePaths.Add(profile, database.FullName);
                    ProfilesList.Items.Add(profile);
                    Profiles.Add(profile);
                }
            }
            catch{}
        }

        private void CancelButtonClick(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void OKButtonClick(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

        private void ProfileListSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            var selectedProfile = ProfilesList.SelectedItem.ToString();
            DatabasePathBox.Text = _profilePaths[selectedProfile];
        }

        private void DatabasePathBoxTextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            FilePath = DatabasePathBox.Text;
        }

        private void BrowseButtonClick(object sender, RoutedEventArgs e)
        {
            var openFileDialog = new OpenFileDialog
                                     {
                                         InitialDirectory =
                                             Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" +
                                             "Skype"
                                     };
            var dialogResult = openFileDialog.ShowDialog();
            if(dialogResult.Value)
                DatabasePathBox.Text = openFileDialog.FileName;
            
        }
    }
}
